package qidi.gushi;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import static android.media.MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED;
/**
 韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha
 on 2018/2/4.
 */
public class LuYin
{
    /**
     * 用于语音播放
     */
    public static MediaPlayer mPlayer = new MediaPlayer();
    /**
     * 用于完成录音
     */
    private static MediaRecorder mRecorder = null;



    /**播放录音*/
    public static void playVoice(){
        try {
            mPlayer.reset();
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
            // ZSYApplication.toast("播放失败");
        }
        //播放结束后，修改控件显示的文字。
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
        {
            @Override
            public void onCompletion(MediaPlayer mp)
            {
                MainActivity.shiting.setText("试听作品");
            }
        });
    }

//    mRecorder.stop();
//    mRecorder.release();
//    mRecorder = null;

    static {

    }
    private static String LOG_TAG = "录音";
    /** 语音文件保存路径 */
    private static String mFileName = null;
    /** 录音存储路径 */
    private static final String PATH = "/sdcard/MyVoiceForder/Record/";
    /**开始时间*/
    static long startTime;
    /** 开始录音 */
    public static void startVoice() {
        //获取开始时间
        startTime = System.currentTimeMillis();
        // 设置录音保存路径
        mFileName = PATH + UUID.randomUUID().toString() + ".amr";
        //获取存储卡状态
        String state = Environment.getExternalStorageState();
        if (!state.equals(Environment.MEDIA_MOUNTED)) {
            LogUtils.i(LOG_TAG, "SD Card is not mounted,It is  " + state + ".");
        }
        //文件目录。不存在，或者目录创建失败
        File directory = new File(mFileName).getParentFile();
        if (!directory.exists() && !directory.mkdirs()) {
            LogUtils.i(LOG_TAG, "Path to file could not be created");
        }
        // 创建录音对象
        mRecorder = new MediaRecorder();
        //
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        //
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        // 录音最长时间
        mRecorder.setMaxDuration(60 * 1000);
        //
        mRecorder.setOutputFile(mFileName);
        //
        mRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {
                if (what == MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
//                    ZSYApplication.toast("单条语音最多60秒，已结束录音");
//                    if (CustomDialogManager.dialog != null)
//                        CustomDialogManager.dialog.dismiss();
                    mRecorder.stop();
                    mRecorder.release();
                    mRecorder = null;
//                    VoiceBean voiceBean = new VoiceBean(mFileName, 60);
//                    mVoicesBeanList.add(voiceBean);
//                    mAdapter.notifyDataSetChanged();
                }
            }
        });
        try {
            mRecorder.prepare();
        } catch (IOException e) {
            LogUtils.e(LOG_TAG, "prepare() failed");
        }
        try {
            mRecorder.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
    /**结束录音*/
    public static void stopVioce()
    {
        if(mRecorder == null) {
            return;
        }
        mRecorder.stop();
    }
    /**结束播放*/
    public static void stopPlayVoice()
    {
        mPlayer.stop();
    }
}
